﻿using Sustainalytics.ClientDW.Synchronization.SyncESGData.AbstractClassesAndInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sustainalytics.SharedEntities.EsgResearch;
using Sustainalytics.ClientDW.Synchronization.SyncESGData.CorporateDataLogic;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData.CompanyWeightsLogic
{
    public class CompanyWeightsOrchestrator : IComprehensiveEsgSyncOrchestrator<EsgResearchProfile>
    {
        private readonly CompanyWeightsMapper companyWeightsMapper;
        private readonly CompanyWeightsCommand companyWeightsCommand;

        public CompanyWeightsOrchestrator(CompanyWeightsMapper companyWeightsMapper, CompanyWeightsCommand companyWeightsCommand)
        {
            this.companyWeightsMapper = companyWeightsMapper;
            this.companyWeightsCommand = companyWeightsCommand;
        }

        async Task IComprehensiveEsgSyncOrchestrator<EsgResearchProfile>.OrchestrateAsync(IList<EsgResearchProfile> comprehensiveEsgResearchProfiles, IList<Guid> invalidatedCompaniesIds)
        {
            var defaultWeightMatrixId = Guid.Empty;

            var companyWeights = companyWeightsMapper.MapManyToMany(comprehensiveEsgResearchProfiles).ToList();

            companyWeights.ForEach(cw => cw.MatrixId = defaultWeightMatrixId);

            await companyWeightsCommand.ExecuteAsync(companyWeights, null, defaultWeightMatrixId);
        }
    }
}
